package com.ncu702.backend.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ncu702.backend.common.BaseResponse;
import com.ncu702.backend.common.ErrorCode;
import com.ncu702.backend.common.ResultUtils;
import com.ncu702.backend.model.dto.articles.ArticleVo;
import com.ncu702.backend.model.dto.user.UserVo;
import com.ncu702.backend.model.entity.User;
import com.ncu702.backend.model.untils.TokeConfig;
import com.ncu702.backend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import  javax.annotation.Resource;
import  javax.servlet.http.HttpServletRequest;
import java.util.List;



@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {
    private  static final String SALT = "kafka";
    @Resource
    private UserService userService;
    @Resource
    private TokeConfig tokeConfig;


    /**
     * 获取用户资料
     */
    @GetMapping("/info")
    public BaseResponse<User> UserByUid(HttpServletRequest httpServletRequest){
        String jwtToken = httpServletRequest.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if(!checkToke){
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,"token失效");
        }
        //通过jwt获取uid
        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        User user = userService.UserByUid(tokeMessage.getUid());
        return ResultUtils.success(user);
    }

    /**
     * 修改用户资料
     */
    @PostMapping("/updateInfo")
    public BaseResponse<Integer> updateInfoByUid(@RequestBody User user, HttpServletRequest httpServletRequest){
        //获取token
        String jwtToken = httpServletRequest.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        //判断 toke 是否过期获取没有
        if(!checkToke){
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,"token失效");
        }
        //获取信息
        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        Long uid = tokeMessage.getUid();
        int i = userService.updateInfoByUid(uid, user);
        return ResultUtils.success(i);
    }


    /*
    修改密码
     */
    @PostMapping("/password1")
    public BaseResponse<Integer> updatePassword1(
            @RequestParam String password,
            HttpServletRequest httpServletRequest){
        //获取token
        String jwtToken = httpServletRequest.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        //判断 toke 是否过期获取没有
        if(!checkToke){
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,"token失效");
        }
        //获取信息
        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        Long uid = tokeMessage.getUid();
        String pass=tokeMessage.getUserPassword();
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT +password).getBytes());
        System.out.println(pass);
        System.out.println(encryptPassword);




        //旧密码是对的才可以
        if(!pass.equals(encryptPassword)){
            return ResultUtils.error(ErrorCode.WRONG_PASSWORD,"旧密码错误");
        }else{
            return ResultUtils.success(1);
        }
    }

    @PostMapping("/password2")
    public BaseResponse<Integer> updatePassword2(
            @RequestParam String password,
            HttpServletRequest httpServletRequest){
        //获取token
        String jwtToken = httpServletRequest.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        //判断 toke 是否过期获取没有
        if(!checkToke){
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,"token失效");
        }
        //获取信息
        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        Long uid = tokeMessage.getUid();
        String pass=tokeMessage.getUserPassword();
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT +password).getBytes());
        System.out.println(pass);
        System.out.println(encryptPassword);


        //旧密码是对的才可以
        if(pass.equals(encryptPassword)){
            return ResultUtils.error(ErrorCode.WRONG_PASSWORD,"新密码和旧密码不能相同");
        }else{
            Integer i=userService.updatePass(uid,encryptPassword);
            return ResultUtils.success(i);
        }
    }


    /*
    根据uid查询单个用户
 */
    @GetMapping("/one")
    public BaseResponse<User> getUserById(
           @RequestParam long uid
    ) {
        User userByUid = userService.UserByUid(uid);
        return ResultUtils.success(userByUid);
    }


    /*
    查询所有用户
     */
    @GetMapping("/all")
    public BaseResponse<PageInfo<User>> getAllUser(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        PageInfo<User> allArticleHome = userService.getAllUser(pageNum, pageSize);
        return ResultUtils.success(allArticleHome);
    }



    /*
    删除用户
     */
    @GetMapping("/delete")
    public BaseResponse<Integer> DeleteUserByUid(
            HttpServletRequest httpServletRequest,
            @RequestParam long uid
    ){
        String jwtToken = httpServletRequest.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if(!checkToke){
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,"token失效");
        }
        //通过jwt获取uid
        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        long uuid=tokeMessage.getUid();
        String role=userService.UserByUid(uuid).getUserRole();
        if(!role.equals("admin")){
            return ResultUtils.error(ErrorCode.FORBIDDEN_ERROR,"you are not admin!");
        }


        Integer user = userService.DeleteUserByUid(uid);
        return ResultUtils.success(user);
    }


    /*
    修改用户角色：user/admin/ban',传一个整数表示要修改成什么权限
     */
    @GetMapping("/role")
    public BaseResponse<Integer> RoleUserByUid(
            HttpServletRequest httpServletRequest,
            @RequestParam long uid,
            @RequestParam Integer op
    ){
        String jwtToken = httpServletRequest.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if(!checkToke){
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR,"token失效");
        }
        //通过jwt获取uid
        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        long uuid=tokeMessage.getUid();
        String role=userService.UserByUid(uuid).getUserRole();
        if(!role.equals("admin")){
            return ResultUtils.error(ErrorCode.FORBIDDEN_ERROR,"you are not admin!");
        }

        Integer user = userService.RoleUserByUid(uid,op);
        return ResultUtils.success(user);
    }



}
